<?php

class m_project extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_project() {
        $sql = "SELECT COUNT(project_id)'total' FROM project";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 

    // get user profil
    function get_all_project_pagination($params) {
        $sql = "SELECT a.*, b.petugas_name'pm', c.klien_nama, d.perusahaan_nama
                FROM project a JOIN com_user b ON a.project_pm = b.user_id
                JOIN klien c ON c.klien_id = a.klien_id
                JOIN perusahaan d ON d.perusahaan_id = a.perusahaan_id
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 
    
    function get_count_all_project_by_pm($params) {
        $sql = "SELECT COUNT(project_id)'total' FROM project WHERE project_pm = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 
    
    // get user profil
    function get_all_project_pagination_by_pm($params) {
        $sql = "SELECT a.*, b.petugas_name'pm', c.klien_nama, d.perusahaan_nama
                FROM project a JOIN com_user b ON a.project_pm = b.user_id
                JOIN klien c ON c.klien_id = a.klien_id
                JOIN perusahaan d ON d.perusahaan_id = a.perusahaan_id
                WHERE project_pm = ?
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 

    function get_project_by_id($params) {
        $sql = "SELECT * FROM project WHERE project_id = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
    function is_exist_nama($params) {
        $sql = "SELECT * FROM project WHERE project_name = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $query->free_result();
            return true;
        } else {
            return false;
        }
    } 

    // insert
    function insert($params) {
        // update by this date
        $sql = "INSERT INTO project(project_name, project_pm, project_startdate, project_enddate, 
                                  project_description, project_kak, project_cpklien, project_status, 
                                  project_datestart, project_dateclose, project_sosialisasi, project_pelatihan,
                                  perusahaan_id, klien_id, mdb, mdd) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $params);
    }

    // edit
    function edit($params) {
        // update by this date
        $sql = "UPDATE project SET  project_name = ?, project_pm = ?, 
                                    project_startdate = ?, project_enddate = ?, 
                                    project_description = ?, project_kak = ?, 
                                    project_cpklien = ?, project_sosialisasi = ?, 
                                    project_pelatihan = ?, perusahaan_id = ?, 
                                    klien_id = ?, mdb = ? 
               WHERE project_id = ?";
        return $this->db->query($sql, $params);
    }

    // delete
    function delete($params) {
        // update by this date
        $sql = "DELETE FROM project WHERE project_id = ?";
        return $this->db->query($sql, $params);
    }

} 